//
//  ZWLyricView.m
//  08.音乐播放器
//
//  Created by mac on 16/3/16.
//  Copyright © 2016年 itheima. All rights reserved.
//

#import "ZWLyricView.h"
#import "Masonry.h"
#import "ZWLyricModel.h"
#import "ZWColorLabel.h"
#import "ZWSlideView.h"


@interface ZWLyricView()<UIScrollViewDelegate>
//横向
@property (weak,nonatomic) UIScrollView *hScrollView;
//纵向
@property (weak,nonatomic) UIScrollView *vScrollView;

@property (weak,nonatomic) ZWSlideView *slideView;
@end

@implementation ZWLyricView
//生成一个_currentLyricIndex的 成员变量
@synthesize currentLyricIndex = _currentLyricIndex;

//通过storyboard创建时加载
- (instancetype)initWithCoder:(NSCoder *)aDecoder{

    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}
//通过代码创建时加载
- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}
- (void)setup{

    UIScrollView *hScrollView = [[UIScrollView alloc]init];
    UIScrollView *vScrollView = [[UIScrollView alloc]init];
    self.hScrollView = hScrollView;
    self.vScrollView = vScrollView;
    
    
//    self.hScrollView.backgroundColor = [UIColor redColor];
    self.vScrollView.backgroundColor = [UIColor yellowColor];
    
    [self addSubview:hScrollView];
    [self.hScrollView addSubview:vScrollView];
    
    self.hScrollView.delegate = self;
    
    ZWSlideView *slideView = [[ZWSlideView alloc]init];
    self.slideView = slideView;
        self.slideView.backgroundColor = [UIColor redColor];
    [self addSubview:self.slideView];
}
- (void)layoutSubviews{

    [super layoutSubviews];
    //添加约束
    [self.hScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    //设置属性
    self.hScrollView.contentSize = CGSizeMake(self.bounds.size.width * 2, 0);
    self.hScrollView.showsHorizontalScrollIndicator = NO;
    self.hScrollView.pagingEnabled = YES;
    self.hScrollView.bounces = NO;
    
    self.vScrollView.contentSize = CGSizeMake(0, self.rowHeight * self.lyricArray.count);
    self.vScrollView.showsVerticalScrollIndicator = NO;

    
    [self.vScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.frame.size.width);
        make.top.bottom.mas_equalTo(self);
        make.width.mas_equalTo(self.bounds.size.width);
    }];
    
    CGFloat top = self.bounds.size.height * 0.5;
    CGFloat bottom = top;
    self.vScrollView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    
    
    

    [self.slideView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.width.mas_equalTo(self);
        make.height.mas_equalTo(44);
    }];
}
#pragma mark - scrollView代理 监听scrollView的滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat progress = scrollView.contentOffset.x / self.bounds.size.width;

    //代理传值
    if ([self.delegate respondsToSelector:@selector(lyricView:scrollViewDidScroll:)]) {
        [self.delegate lyricView:self scrollViewDidScroll:progress];
    }
}

- (void)setLyricArray:(NSArray *)lyricArray{

    _lyricArray = lyricArray;
    
    [self.vScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    for (int i=0; i<self.lyricArray.count; i++) {
        
        ZWLyricModel *lyricModel = self.lyricArray[i];
        ZWColorLabel *label = [[ZWColorLabel alloc]init];
        label.text = lyricModel.lyricString;
//        [label sizeToFit];
        [self.vScrollView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self);
            make.top.mas_equalTo(self.rowHeight * i);
        }];

    }
}

- (CGFloat)rowHeight{

    if (_rowHeight == 0) {
        _rowHeight = 44;
    }
    return _rowHeight;
}

- (void)setProgress:(CGFloat)progress{

    _progress = progress;
    
    ZWColorLabel *label = self.vScrollView.subviews[self.currentLyricIndex];
    label.progress = progress;
}

- (void)setCurrentLyricIndex:(NSInteger)currentLyricIndex{

    ZWColorLabel *preLabel = self.vScrollView.subviews[_currentLyricIndex];
    preLabel.font = [UIFont systemFontOfSize:17];
    preLabel.progress = 0;
    _currentLyricIndex = currentLyricIndex;
    ZWColorLabel *label = self.vScrollView.subviews[currentLyricIndex];
    label.font = [UIFont systemFontOfSize:30];
    
    CGFloat offsetY = self.rowHeight * self.currentLyricIndex - self.vScrollView.contentInset.top;
    [UIView animateWithDuration:0.5 animations:^{
        self.vScrollView.contentOffset = CGPointMake(0, offsetY);}];
}

- (NSInteger)currentLyricIndex{

    if (_currentLyricIndex <0 ) {
        _currentLyricIndex = 0;
    }else if(_currentLyricIndex > self.lyricArray.count - 1){
        _currentLyricIndex = self.lyricArray.count - 1;
    }
    return _currentLyricIndex;
}

@end
